<template>
  <basic-dialog
    :visible="dialogVisible"
    :title="'原材料-处理意见填写'"
    :width="'65%'"
    :handleClose="handleCloseDialog"
    :handleOpen="handleOpenDialog"
    :handleOk="handleSubmit"
    :loadingBtn="submitBtnLoading"
    :okText="'提交'"
  >
    <el-form
      ref="auditForm"
      :model="auditForm"
      :rules="formValidate"
      label-width="180px"
      v-loading="formLoading"
    >
      <el-form-item label="原材料浮动:">
        <span>{{auditForm.currentFloatRadioStr}} {{auditForm.currentFloatPrice}}元</span>
      </el-form-item>
      <el-form-item :label="auditForm.currentFloatPrice > 0 ? '是否需调整售价:' : '是否需调整进价:'" prop="currentAdjustPriceState">
        <el-select v-model="auditForm.currentAdjustPriceState">
          <el-option
            v-for="item in PRICE_STATE_OPTIONS"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="处理备注说明:" prop="currentProcessRemark">
        <el-input v-model.trim="auditForm.currentProcessRemark" maxlength="200" type="textarea" :rows="6" />
      </el-form-item>
      <el-form-item label="连续预警未调价次数:">
        <span>{{auditForm.currentUnAdjustPriceTimes}}</span>
      </el-form-item>
      <el-form-item label="上次材料浮动:">
        <span>{{auditForm.floatRadioStr}} {{auditForm.floatPrice}}元</span>
      </el-form-item>
      <el-form-item label="上次是否需调整售价/进价:">
        <span>{{auditForm.adjustPriceStateStr}}</span>
      </el-form-item>
      <el-form-item label="上次处理备注说明:">
        <span>{{auditForm.processRemark}}</span>
      </el-form-item>
      <el-form-item label="上次处理人:">
        <span>{{auditForm.operName}}</span>
      </el-form-item>
      <el-form-item label="上次处理时间:">
        <span>{{auditForm.opeTime}}</span>
      </el-form-item>
      <el-form-item label="上次进价调整时间:">
        <span>{{auditForm.adjustPriceTime}}</span>
      </el-form-item>
    </el-form>
  </basic-dialog>
</template>
<script>

import { setAuthBtnId } from '@/utils/bizUtils';
import BasicDialog from '@/components/BasicDialog';
import { PRICE_STATE_OPTIONS } from '../constants'
import { queryMaterialToDoDetailService, handleProcessMaterialService } from '@/api/finance/rawMaterialManage/toDoList';

export default {
  name: 'DisposeDialog', // 处理待办
  components: {
    BasicDialog
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    todoId: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      formLoading: false,
      submitBtnLoading: false,
      PRICE_STATE_OPTIONS,
      auditForm: {
        currentFloatRadio: '',
        currentFloatPrice: '',
        currentAdjustPriceState: '', // 1 是 2 否
        currentProcessRemark: '',
        currentUnAdjustPriceTimes: '',
        floatRadio: '',
        floatPrice: '',
        adjustPriceStateStr: '',
        processRemark: '',
        operName: '',
        opeTime: '',
        adjustPriceTime: '',
      },
      formValidate: {
        currentAdjustPriceState: [
          { required: true, message: '请选择是否需调整售价/进价', trigger: 'change' }
        ],
        currentProcessRemark: [
          { required: true, message: '请输入处理备注说明', trigger: 'blur' },
          { type: 'string', min: 1, max: 200, message: '最多输入200字', trigger: 'blur' }
        ],
      },
    };
  },
  computed: {
    dialogVisible () {
      return this.visible;
    },
    uuid () {
      return this.todoId;
    }
  },
  methods: {
    handleSubmit () {
      this.$refs.auditForm.validate((valid) => {
        if (valid) {
          this.handleSubmitAction()
        }
      });
    },
    async handleSubmitAction () {
      const { currentAdjustPriceState, currentProcessRemark } = this.auditForm
      const data = {
        uuid: this.uuid,
        adjustPriceState: currentAdjustPriceState,
        processRemark: currentProcessRemark
      }
      const res = await handleProcessMaterialService(
        data,
        state => { this.submitBtnLoading = state }
      );
      if (res) {
        this.$message.success('提交成功')
        // 是调整进价
        this.$emit('disposeCallback', { currentAdjustPriceState });
        this.handleCloseDialog()
      }
    },
    // 获取详情数据
    async getDetailData () {
      setAuthBtnId();
      const data = {
        uuid: this.uuid,
        rawMaterialProcessType: 1 // 处理
      };
      const res = await queryMaterialToDoDetailService(
        data,
        state => { this.formLoading = state }
      );
      if (res?.retData) {
        this.auditForm = res.retData;
      }
    },
    // 打开弹窗
    handleOpenDialog () {
      this.getDetailData()
    },
    // 关闭弹框
    handleCloseDialog () {
      this.$emit('update:visible', false);
    },
  }
};
</script>
